<?php
namespace app\admin\controller;

use app\common\model\AuthGroup as AuthGroupModel;
use app\common\model\AuthRule as AuthRuleModel;
use app\common\controller\AdminBase;

/**
 * 权限组
 * Class AuthGroup
 * @package app\admin\controller
 */
class AuthGroup extends AdminBase
{
    protected $auth_group_model;
    protected $auth_rule_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->auth_group_model = new AuthGroupModel();
        $this->auth_rule_model  = new AuthRuleModel();
    }

    /**
     * 权限组
     * @return mixed
     */
    public function index()
    {
        $auth_group_list = $this->auth_group_model->select();

        return $this->fetch('index', ['auth_group_list' => $auth_group_list]);
    }

    /**
     * 添加权限组
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存权限组
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($this->auth_group_model->save($data) !== false) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
    }

    /**
     * 编辑权限组
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $auth_group = $this->auth_group_model->find($id);

        return $this->fetch('edit', ['auth_group' => $auth_group]);
    }

    /**
     * 更新权限组
     * @param $id
     */
    public function update($id,$status)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($id == 1 && $data['status'] != 1) {
                $this->error('超级管理组不可禁用');
            }
            if ($this->auth_group_model->save($data, $id) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }else{
            if ($id == 1 && $status != 1) {
                $this->error('超级管理组不可禁用');
            }
            $auth_group              = $this->auth_group_model->find($id);
            $auth_group->id          = $id;
            $auth_group->status   = $status;
            if ($auth_group->save() !== false) {
                $this->success('状态更新成功');
            } else {
                $this->error('状态更新失败');
            }
        }
    }

    /**
     * 删除权限组
     * @param $id
     */
    public function delete($id)
    {
        if ($id == 1) {
            $this->error('超级管理组不可删除');
        }
        if ($this->auth_group_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 授权
     * @param $id
     * @return mixed
     */
    public function auth($id)
    {
        //已选权限调用
        $auth_group_data = $this->auth_group_model->find($id)->toArray();
        $auth_rules      = explode(',', $auth_group_data['rules']);

        //一级分类
        $onewhere = [];
        $onewhere['pid'] = 0;
        $one_auth_rule = $this->auth_rule_model->where($onewhere)->order(['sort' => 'ASC', 'id' => 'ASC'])->select();
        //二级分类
        $two_auth_rule_list = [];//初始化
        foreach ($one_auth_rule as $k => $v) {
            $twowhere = [];
            $twowhere['pid'] = $v['id'];
            $two_auth_rule = $this->auth_rule_model->where($twowhere)->order(['sort' => 'ASC', 'id' => 'ASC'])->select();
            //三级分类
            $three_auth_rule_list = [];//初始化
            foreach ($two_auth_rule as $kk => $vv) {
                $threewhere = [];
                $threewhere['pid'] = $vv['id'];
                $three_auth_rule = $this->auth_rule_model->where($threewhere)->order(['sort' => 'ASC', 'id' => 'ASC'])->select();
                //四级分类
                $four_auth_rule_list = [];//初始化
                foreach ($three_auth_rule as $kkk => $vvv) {
                    $fourwhere = [];
                    $fourwhere['pid'] = $vvv['id'];
                    $four_auth_rule = $this->auth_rule_model->where($fourwhere)->order(['sort' => 'ASC', 'id' => 'ASC'])->select();
                    $four_auth = [];//初始化
                    foreach ($four_auth_rule as $kkkk => $vvvv) {
                        if (in_array($vvvv['id'], $auth_rules)){
                            $vvvv['checked'] = 1;
                        }else{
                            $vvvv['checked'] = 0;
                        }
                        $four_auth[]=$vvvv;
                    }
                    $vvv["four"]=$four_auth;
                    if (in_array($vvv['id'], $auth_rules)){
                        $vvv['checked'] = 1;
                    }else{
                        $vvv['checked'] = 0;
                    }
                    $four_auth_rule_list[]=$vvv;
                }
                $vv["three"]=$four_auth_rule_list;
                if (in_array($vv['id'], $auth_rules)){
                    $vv['checked'] = 1;
                }else{
                    $vv['checked'] = 0;
                }
                $three_auth_rule_list[]=$vv;
            }
            $v["two"]=$three_auth_rule_list;
            if (in_array($v['id'], $auth_rules)){
                $v['checked'] = 1;
            }else{
                $v['checked'] = 0;
            }
            $one_auth_rule_list[]=$v;
        }
        $auth_rule_list=$one_auth_rule_list;

        return $this->fetch('auth', ['id' => $id ,'auth_rule_list' => $auth_rule_list]);
    }

    /**
     * 更新权限组规则
     * @param $id
     * @param $auth_rule_ids
     */
    public function updateAuthGroupRule()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $group_data['id']    = $data['group_id'];
            $group_data['rules'] = is_array($data['menu']) ? implode(',', $data['menu']) : '';
            if ($this->auth_group_model->save($group_data, $group_data['id']) !== false) {
                $this->success('授权成功');
            } else {
                $this->error('授权失败');
            }
        }
    }
}